"use strict";
/* Controllers */
app.controller("dataMapItemController", ['$scope', '$location', '$rootScope', '$routeParams', 'dataMapItemService', function ($scope, $location, $rootScope, $routeParams, dataMapItemService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    vm.p.type = $routeParams.type;
    if(vm.p.type=='id') {
        vm.p.id = $routeParams.id;
    }else {
        vm.p.topicTbl = $routeParams.id;
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.getMetricTopic = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 请求参数
        var params = {
            id: vm.p.id,
            topicTbl: vm.p.topicTbl
        };

        // 结果处理
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            vm.p.id = data.id; // topicTbl需要设置
            $('#list_table').bootstrapTable('load', data.relationEntityList);
        };
        dataMapItemService.getMetricTopic(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageItem: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.getMetricTopic();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('dataMapItemService', ['ajaxService', function (ajaxService) {

    this.getMetricTopic = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/data_map/getMetricSource.do", successFunction, errorFunction);
    };
}]);


function topicTblIdOptFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="https://dp.58corp.com/data-map/detail-page/', row.topicTblId,'" target="_blank">', "<u>DP详情</u>" , '</a>'
    ].join('');
}